﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class tools_Functional_Area_Master : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (!IsPostBack)
        {
            try
            {
                clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
                FillFuntioalArea();
//FillddlState();
            }
            catch (Exception)
            {
                throw;
            }
        
    }
    }
    protected void btnfunctionalareasave_Click(object sender, EventArgs e)
    {
        try
        {
            if (Session["FuntioalAreaId"] == null)
            {

                clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
                IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
                objIN_TB_FUNCTIONAL_AREA.FUNCTIONAL_AREA = txtfunctionalareaname.Text;
                int FuntioalAreaId = objIN_TB_FUNCTIONAL_AREAD.SaveFunctionalMst(objIN_TB_FUNCTIONAL_AREA);
                //ClearAllControls();
                FillFuntioalArea();
            }
            else
            {
                clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
                IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
                objIN_TB_FUNCTIONAL_AREA.ID = Convert.ToInt32(Session["FuntioalAreaId"].ToString());
                objIN_TB_FUNCTIONAL_AREA.FUNCTIONAL_AREA = txtfunctionalareaname.Text;
                int FuntioalAreaId = objIN_TB_FUNCTIONAL_AREAD.SaveFunctionalMst(objIN_TB_FUNCTIONAL_AREA);
                FillFuntioalArea();
               // ClearAllControls();
                Session["FuntioalAreaId"] = null;
            }
            txtfunctionalareaname.Text = "";
        }

        catch (Exception)
        {
            throw;
        }
    }
    protected void FillFuntioalArea()
    {
        try
        {
            List<IN_TB_FUNCTIONAL_AREA> objIN_TB_FUNCTIONAL_AREAist = new List<IN_TB_FUNCTIONAL_AREA>();
            clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
            objIN_TB_FUNCTIONAL_AREAist = objIN_TB_FUNCTIONAL_AREAD.GetAllFunctionalAreas();
            if (objIN_TB_FUNCTIONAL_AREAist != null)
            {
                grdFuntionalArea.DataSource = objIN_TB_FUNCTIONAL_AREAist;
                grdFuntionalArea.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdFuntionalArea_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdFuntionalArea.PageIndex = e.NewPageIndex;
        FillFuntioalArea();

    }
    protected void grdFuntionalArea_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int FuntioalAreaId = Convert.ToInt32(e.CommandArgument.ToString());
                clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
                IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
                objIN_TB_FUNCTIONAL_AREA = objIN_TB_FUNCTIONAL_AREAD.GetAllFunctionalAreaById(FuntioalAreaId);
                if (objIN_TB_FUNCTIONAL_AREA != null)
                {

                   txtfunctionalareaname.Text = objIN_TB_FUNCTIONAL_AREA.FUNCTIONAL_AREA;
                    Session["FuntioalAreaId"] = FuntioalAreaId;
                }
            }

            if (e.CommandName == "Del")
            {
                int FuntioalAreaId = Convert.ToInt32(e.CommandArgument.ToString());
                clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
                IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
                objIN_TB_FUNCTIONAL_AREAD.DeleteFUNCTIONALAREA(FuntioalAreaId);
                FillFuntioalArea();
            }
        }
        catch (Exception)
        {

            throw;
        }

    }
    protected void grdFuntionalArea_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //try
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        //Getting State Names
        //        int FuntioalAreaId = Convert.ToInt32(e.Row.Cells[o].Text);
        //        clsFunctionalAreaDAL objIN_TB_FUNCTIONAL_AREAD = new clsFunctionalAreaDAL();
        //        IN_TB_FUNCTIONAL_AREA objIN_TB_FUNCTIONAL_AREA = new IN_TB_FUNCTIONAL_AREA();
        //        objIN_TB_FUNCTIONAL_AREA = objIN_TB_FUNCTIONAL_AREAD.GetAllFunctionalAreaById(StateId);
        //        e.Row.Cells[1].Text = objIN_TB_STATE.STATE;

        //    }
        //}
        //catch (Exception)
        //{

        //    throw;
        //}

    }
}
